package com.headwind.logistics.order.controller;

import com.headwind.logistics.common.pojo.Employee;
import com.headwind.logistics.common.util.LayuiUtil;
import com.headwind.logistics.common.util.RestResult;
import com.headwind.logistics.common.util.ResultGenerator;
import com.headwind.logistics.order.dto.PlanOrderAddressCustomerDto;
import com.headwind.logistics.common.pojo.BaseNetSite;
import com.headwind.logistics.order.entity.OrderFeeType;
import com.headwind.logistics.order.entity.OrderPack;
import com.headwind.logistics.order.entity.OrderServiceFee;
import com.headwind.logistics.order.entity.PlanOrder;
import com.headwind.logistics.order.service.inter.PlanOrderServiceInter;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author 盛夏
 * @Date 2020-07-15 下午 4:08
 * @Version 1.0
 * @DESCRIPTION 预录单的控制器
 */
@Controller
public class PlanOrderController {

    @Resource
    private PlanOrderServiceInter planServiceInter;

    @Resource
    private ResultGenerator resultGenerator;

    @RequestMapping("/order/test")
    public String test(){
        return "test";
    }

    @RequestMapping("/order/add")
    public String add(){
        return "add";
    }

    /**
     * 获取包装类型
     * @return  返回包装类型集合
     */
    @ResponseBody
    @GetMapping("/planOrder/getBackType")
    public List<OrderPack> getBackType(){
        List<OrderPack> orderPacks = planServiceInter.queryOrderPack();

        return  orderPacks;
    }

    /**
     * 获取费用类型
     * @return  费用类型集合
     */
    @ResponseBody
    @GetMapping("/planOrder/getFeeType")
    public RestResult getFeeType(){
        List<OrderFeeType> feeTypes = planServiceInter.queryOderFeeType();

        return resultGenerator.getSuccessResult(feeTypes);
    }

    /**
     * 查询所有预录单并分页
     * @param page  第几条数据
     * @param limit 当前页
     * @return 数据表格格式
     */
    @ResponseBody
    @RequestMapping("/admin/planOrder/findAllPlanOrder")
    public LayuiUtil findAllPlanOrder(Integer page, Integer limit, String orderTime, String planOdd,
                                      String phone, String username, HttpSession session){
        int count = planServiceInter.queryPlanOrderCount();
        Long empId = ((Employee) session.getAttribute("EMPLOYEE")).getEmployeeId();
        List<PlanOrderAddressCustomerDto> planOrders = new ArrayList<>();
        if (page == null && limit == null){
            planOrders = planServiceInter.queryPlanOrderPage(0, 2,username,planOdd,phone,orderTime,empId);
        }else{
            planOrders = planServiceInter.queryPlanOrderPage(((page-1) * limit), limit,username,planOdd,phone,orderTime,empId);
        }

        return LayuiUtil.data(count,planOrders);
    }

    /**
     * 打开弹出层后查询网点信息
     * @param putArea   收货地址
     * @param sendArea      寄货地址
     * @return          网点的hashMap集合
     */
    @ResponseBody
    @PostMapping("/planOrder/popups")
    public HashMap<String,List<BaseNetSite>> find(String sendArea, String putArea){

        return planServiceInter.queryBaseNetSiteById(sendArea,putArea);
    }

    /**
     * 根据网点ID得到快递单号
     * @param siteID
     * @return
     */
    @ResponseBody
    @GetMapping("/admin/planOrder/getExpressGrant")
    public RestResult getExpressGrant(String siteID){

        return resultGenerator.getSuccessResult(planServiceInter.queryExpressGrant(siteID));
    }

    /**
     * 根据预录单号查预录单数据
     * @param planOrderOdd  预录单号
     * @return
     */
    @ResponseBody
    @GetMapping("/admin/planOrder/getPlanDto")
    public RestResult getPlanDto(String planOrderOdd){
        PlanOrderAddressCustomerDto planDto = planServiceInter.queryPlanOrderByOdd(planOrderOdd);
        System.out.println(planDto.getRecordedNumber());
        return resultGenerator.getSuccessResult(planDto);
    }

    @ResponseBody
    @PostMapping("/admin/planOrder/deletePlanOrder")
    public RestResult deletePlanOrder(String planOrderOdd,HttpSession session){


        return resultGenerator.getSuccessResult(planServiceInter.logicDeletePlanOrderByOdd(planOrderOdd,session));
    }

    /**
     * 修改预录单信息
     * @param planDto   预录单数据传输对象
     * @return
     */
    @ResponseBody
    @PostMapping("/admin/planOrder/updatePlanOrder")
    public int updatePlanOrder(PlanOrderAddressCustomerDto planDto){

        return planServiceInter.updatePlanOrderMessage(planDto);
    }
}
